package com.platform.usercenter.teamfc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.platform.common.core.domain.entity.SysDept;
import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.usercenter.domain.CalLeaderPlan;
import com.platform.usercenter.domain.SysMenu;
import com.platform.usercenter.domain.SysOrganizationDept;
import com.platform.usercenter.domain.dto.CalLeaderPlanDTO;
import com.platform.usercenter.domain.vo.SysOrgDeptUserVO;
import com.platform.usercenter.domain.vo.SysUserVO;
import com.platform.usercenter.domain.vo.UserVo;
import com.platform.usercenter.mapper.*;
import com.platform.usercenter.service.ISysDeptService;
import com.platform.usercenter.teamfc.domain.dto.MenuDTO;
import com.platform.usercenter.teamfc.domain.vo.SysDeptTreeVO;
import com.platform.usercenter.teamfc.mapper.PlatUserCenterFcMapper;
import com.platform.usercenter.teamfc.service.PlatUserCenterFcService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PlatUserCenterFcServiceImpl implements PlatUserCenterFcService {

	@Resource
	private PlatUserCenterFcMapper platUserCenterFcMapper;

	@Resource
	private ISysDeptService sysDeptService;

	@Resource
	private CalLeaderPlanMapper calLeaderPlanMapper;
	@Resource
	private SysDeptMapper deptMapper;

	@Resource
	SysUserMapper userMapper;

	@Resource
	private SysOrganizationDeptMapper sysOrganizationDeptMapper;

	@Resource
	private SysOrgDeptUserMapper sysOrgDeptUserMapper;

	/**
	 * 查询系统菜单列表
	 *
	 * @param menu 菜单信息
	 * @return 菜单列表
	 */
	@Override
	public List< SysMenu > selectMenuList( MenuDTO menu,Long userId,Long orgId ) {
		List< SysMenu > menuList=null;
		// 管理员显示所有菜单信息
		if(SysUser.isAdmin( userId )) {
			menu.setAdminFlag( "1" );
		}else {
			menu.setAdminFlag( "0" );
			menu.setUserId( userId );
			menu.setOrgId( orgId );
		}
		menuList=platUserCenterFcMapper.selectMenuListByUserInfo( menu );
		return menuList;
	}

	@Override
	public List< SysUser > queryUserIdList( Long orgId ) {
		SysUser sysUser=new SysUser( );
		sysUser.setOrgId( orgId );
		return platUserCenterFcMapper.selectUserList( sysUser );
	}

	@Override
	public List< SysUserVO > getUserInfoById( SysUserVO user ) {
		// 处理部门子节点id
		if(CollectionUtil.isNotEmpty( user.getDeptIds( ) )) {
			Set< Long > deptIds=new HashSet<>( user.getDeptIds( ) );
			user.setDeptIds( new ArrayList<>( deptIds ) );
			for(Long deptId: user.getDeptIds( )) {
				deptIds.add( user.getDeptId( ) );
				SysOrganizationDept organizationDept=new SysOrganizationDept( );
				organizationDept.setOrgId( user.getOrgId( ) );
				organizationDept.setDeptId( deptId );
				List< SysOrganizationDept > deptList=sysOrganizationDeptMapper.selectDeptListByParentId( organizationDept );
				if(CollectionUtil.isNotEmpty( deptList )) {
					deptIds.addAll( deptList.stream( ).map( SysOrganizationDept::getDeptId ).collect( Collectors.toSet( ) ) );
				}
			}
			user.setDeptId( null );
		}
		return userMapper.getUserInfoById( user );
	}


	@Override
	public List< SysOrgDeptUserVO > queryUserListByPostIds( SysOrgDeptUserVO sysOrgDeptUserVO ) {

		if(!CollectionUtil.isNotEmpty( sysOrgDeptUserVO.getPostIds( ) )) {
			return Collections.emptyList( );
		}
		return sysOrgDeptUserMapper.selectListByPostIds( sysOrgDeptUserVO );
	}


	@Override
	public List< SysUser > queryUserListByDeptName( String deptName ) {
		List< SysUser > sysUserList=new ArrayList<>( );
		SysDept dept=new SysDept( );
		dept.setDeptName( deptName );
		List< SysDept > sysDeptList=deptMapper.selectDeptList( dept );
		if(CollectionUtil.isNotEmpty( sysDeptList ) && sysDeptList.size( )>0) {

			for(SysDept sysDept: sysDeptList) {
				SysUser sysUser=new SysUser( );
				sysUser.setDeptId( sysDept.getDeptId( ) );
				sysUserList.addAll( platUserCenterFcMapper.selectUserList( sysUser ) );
			}
		}
		return sysUserList;
	}

	@Override
	public List< SysUser > queryUserIdListByUserType( String userType ) {
		SysUser sysUser=new SysUser( );
		sysUser.setUserType( userType );
		return platUserCenterFcMapper.selectUserList( sysUser );
	}

	@Override
	public List< SysDeptTreeVO > queryDeptTree( Long deptId ) {
		List< SysDeptTreeVO > sysDeptTreeVOS=platUserCenterFcMapper.querySysDeptTree( deptId );
		return sysDeptTreeVOS;
	}

	@Override
	public SysDeptTreeVO queryDeptTreeData( Long deptId ) {
		List< SysDept > sysDepts=sysDeptService.selectDeptList( new SysDept( ) );
		List< Long > existId=new ArrayList<>( );
		existId.add( deptId );
		SysDept userNode=sysDepts.stream( ).filter( dept->dept.getDeptId( ).equals( deptId ) ).collect( Collectors.toList( ) ).get( 0 );
		List< SysDeptTreeVO > userChildrenNode=sysDepts.stream( ).filter( dept->dept.getParentId( ).equals( deptId ) ).map( dept->toNode( dept ) ).collect( Collectors.toList( ) );
		SysDeptTreeVO node=toNode( userNode );
		node.setChildren( userChildrenNode );
		if(userChildrenNode!=null && userChildrenNode.size( )>0) {
			List< Long > childrenIds=userChildrenNode.stream( ).map( n->Long.valueOf( n.getId( ) ) ).collect( Collectors.toList( ) );
			existId.addAll( childrenIds );
		}
		getChildrenNode( sysDepts,existId,node );
		return node;
	}

	@Override
	public List< CalLeaderPlan > selectCalLeaderPlanList( CalLeaderPlanDTO calLeaderPlan ) {
		return calLeaderPlanMapper.selectCalLeaderPlanList( calLeaderPlan );
	}

	@Override
	public List< SysUser > getUserListInfo( Long[] userIds ) {
		return userMapper.selectUserByIds( userIds );
	}

	private void getChildrenNode( List< SysDept > allData,List< Long > existNodeIds,SysDeptTreeVO indexNode ) {
		List< SysDeptTreeVO > childrenNode=indexNode.getChildren( );
		if(childrenNode==null || childrenNode.size( )==0) {
			return;
		}else {
			for(SysDeptTreeVO node: childrenNode) {
				String id=node.getId( );
				List< SysDept > childDataList=allData
						.stream( )
						.filter( d->!existNodeIds.contains( d.getDeptId( ) ) )
						.filter( d->d.getParentId( ).equals( Long.valueOf( id ) ) )
						.collect( Collectors.toList( ) );
				if(childDataList!=null && childDataList.size( )>0) {
					List< Long > childIds=childDataList.stream( ).map( SysDept::getDeptId ).collect( Collectors.toList( ) );
					existNodeIds.addAll( childIds );
				}
				List< SysDeptTreeVO > childNodes=childDataList.stream( ).map( n->toNode( n ) ).collect( Collectors.toList( ) );
				node.setChildren( childNodes );
				getChildrenNode( allData,existNodeIds,node );
			}
		}
	}

	private SysDeptTreeVO toNode( SysDept orgNode ) {
		SysDeptTreeVO node=new SysDeptTreeVO( );
		node.setId( orgNode.getDeptId( )+"" );
		node.setLabel( orgNode.getDeptName( ) );
		node.setParentId( orgNode.getParentId( )+"" );
		return node;
	}


	@Override
	public List< SysUser > selectDeptIdUsersWithKeyword( Long orgId,String keyword ) {
		UserVo userVo=new UserVo( );
		userVo.setKeyword( keyword );
		userVo.setOrgId( orgId );
		return platUserCenterFcMapper.selectDeptIdUsersWithKeyword( userVo );
	}

}


